package cloud.xlh.middleware_manage.util;

import cloud.xlh.middleware_manage.enums.TypeEnums;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import redis.clients.jedis.Jedis;

import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/3/17 14:17
 * @ Description: 中间件探活策略工具类
 */
public class KeepAliveStrategyUtil {
    private Integer code;

    /**
     *
     * @param code 中间件种类码
     * @return
     * @author XuLeHuang
     * @create 2023/3/17
     * @name:
     * @Description:
     **/
    public KeepAliveStrategyUtil(Integer code) {
        this.code = code;
    }

    public static KeepAliveStrategyUtil getStrategyInstance(Integer code) {
        return new KeepAliveStrategyUtil(code);
    }

    /**
     *
     * @param hostName
     * @param port
     * @param username
     * @param password
     * @return boolean
     * @author XuLeHuang
     * @create 2023/3/17
     * @name: 策略执行类
     * @Description:
     **/
    public boolean exKeepAliveStrategy(String hostName, int port, String username, String password) {
        if (TypeEnums.REDIS.getCode().equals(this.code)) {
            return redisKeepAliveTest(hostName, port, username, password);
        } else if (TypeEnums.CANAL.getCode().equals(this.code)) {
            return canalKeepAliveTest(hostName, port, username, password);
        } else if (TypeEnums.MYSQL.getCode().equals(this.code)){
            return mysqlKeepAliveTest(hostName, port, username, password);
        }
        return false;
    }

    private boolean redisKeepAliveTest(String hostName, int port, String username, String password) {
        try {
            Jedis jedis = new Jedis(hostName, port);
            String ping = jedis.ping();
            if (ping.equalsIgnoreCase("PONG")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    private boolean canalKeepAliveTest(String hostName, int port, String username, String password) {
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(hostName, port), "example", "", "");
        try {
            connector.connect();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            if ("CanalClientException".equals(e.getClass().getSimpleName()) && "Connection refused: connect".equals(e.getCause().getMessage())) {
                return false;
            }
        } finally {
            connector.disconnect();
        }
        return false;
    }

    private boolean mysqlKeepAliveTest(String hostName, int port, String username, String password) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(hostName, username, password);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

}
